﻿param($localGroupName)

$computers=(Get-ADComputer -Filter {(name -notlike "*-*-*")}).Name | Sort-Object


foreach($computer in $computers)
{
    $scriptBlock= 
    {
        if($using:computer -inotlike "*v-*")
        {
            $connectionTest=Test-Connection -Count 1 -ComputerName $using:computer -ErrorAction SilentlyContinue
            if($connectionTest)
            {
                 
                #if ($computerName -eq "") {$using:computer = "$env:computername"}  
  
                if([ADSI]::Exists("WinNT://$using:computer/$using:localGroupName,group")) {  
  
                    $group = [ADSI]("WinNT://$using:computer/$using:localGroupName,group")  
  
                    $members = @()  
                    $Group.Members() |  
                    % {  
                        $AdsPath = $_.GetType().InvokeMember("Adspath", 'GetProperty', $null, $_, $null)  
                        # Domain members will have an ADSPath like WinNT://DomainName/UserName.  
                        # Local accounts will have a value like WinNT://DomainName/ComputerName/UserName.  
                        $a = $AdsPath.split('/',[StringSplitOptions]::RemoveEmptyEntries)  
                        $name = $a[-1]  
                        $domain = $a[-2]  
                        $class = $_.GetType().InvokeMember("Class", 'GetProperty', $null, $_, $null)  
  
                        $member = New-Object PSObject
                        $member | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $using:computer 
                        $member | Add-Member -MemberType NoteProperty -Name "ParentGroupName" -Value $using:localGroupName
                        $member | Add-Member -MemberType NoteProperty -Name "Name" -Value $name  
                        $member | Add-Member -MemberType NoteProperty -Name "Domain" -Value $domain  
                        $member | Add-Member -MemberType NoteProperty -Name "Class" -Value $class  
  
                        $members += $member  
                    }  
                    if ($members.count -eq 0) {  
                        Write-Host "Group '$using:computer\$$using:localGroupName' is empty."  
                    }  
                    else {  
                        
                        $members | Export-Csv -NoTypeInformation -Path C:\temp\LocalAdministratorsMembers\$using:computer`_$using:localGroupName.csv -Force
                    }  
                }  
                else {  
                    Write-Warning "Local group '$using:localGroupName' doesn't exist on computer '$using:computer'"  
                }
            }
            else
            {
                Write-Output "Cannot reach $using:computer"
            }
        }
        else
        {
            Write-Output "$using:computer is a service name,skipping."
        }
    }


    $activeJobsCount=(Get-Job -Name "Gather local administrators*" | Where {$_.State -eq "Running"} | Measure-Object).Count

    if($activeJobsCount -lt 30)
    {
        Start-Job -Name "Gather local administrators on $computer" -ScriptBlock $scriptBlock
    }
    else
    {
        Start-Sleep -Seconds 3    
    }

    Get-Job "Gather local administrators*" | where {$_.State -eq "Completed"} | Remove-Job
}